package com.example.administrator.wifitest;


import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.os.PowerManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.NumberFormat;

public class MainActivity extends  Activity {

    private TextView rsltv,addrv,itvav;
    private Button strsb,stopb,sendb,clearb;
    int cnt=0,suc=0,stopn=0;
    int rssi=0,itvan=1000;
    private StringBuilder response;
    String addrs="https://www.baidu.com/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.);
        ;
        rsltv = (TextView) findViewById(R.id.rslt);
        addrv= (EditText) findViewById(R.id.addr);
        itvav= (EditText) findViewById(R.id.itva);

        sendb = (Button) findViewById(R.id.send);
        sendb.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                stopn=1;
                String t1=addrv.getText().toString();
                if(t1==null|t1.length()<=0){
                    addrs="https://www.baidu.com/";
                }
                else {
                    addrs=t1;
                }
                sendHttpRequest(addrs);

            }
        });

        strsb = (Button) findViewById(R.id.strs);
        strsb.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                stopn=0;
                String t1=addrv.getText().toString();
                if(t1==null|t1.length()<=0){
                    addrs="https://www.baidu.com/";
                }
                else {
                    addrs=t1;
                }
                sendHttpRequest(addrs);
            }
        });

        stopb = (Button) findViewById(R.id.stop);
        stopb.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                stopn=1;
            }
        });

        clearb = (Button) findViewById(R.id.clear);
        clearb.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                stopn=1;
                suc=0;cnt=0;
                rssi=0;
                String p = percent(suc,cnt);
                rsltv.setText(
                                "\n信号强度RSSI:"+rssi+
                                "\n成功比例:    "+p+ "%"+
                                "\n发送次数:    "+cnt+
                                "\n接收次数:    "+suc+
                                "\n测试地址:    "+addrs+
                                "\n发送间隔:    "+itvan
                );
            }
        });

    }

    public void sendHttpRequest(final String address) {
        AsyncTask<Void, String, String> task = new AsyncTask<Void, String, String>() {
            @Override
            protected void onPostExecute(String result) {

                String p = percent(suc,cnt);
                rsltv.setText(
                        "\n信号强度RSSI:"+rssi+
                        "\n成功比例:    "+p+ "%"+
                        "\n发送次数:    "+cnt+
                        "\n接收次数:    "+suc+
                        "\n测试地址:    "+addrs+
                        "\n发送间隔:    "+itvan
                );
                if(stopn==0)
                {
                    try {
                        Thread.sleep(itvan);//
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    sendHttpRequest(address);}
            }
            @Override
            protected void onPreExecute() {

                String t2=itvav.getText().toString();
                if(t2==null|t2.length()<=0){
                    itvan=1000;
                }
                else if (Integer.parseInt(t2)<=0){
                    itvan=1000;
                }
                else {
                        itvan=Integer.parseInt(t2);
                }

            }
            @Override
            protected void onProgressUpdate(String... value)
            {
//                rsltv.setText(value[0]);
            }
            @Override
            protected String doInBackground(Void... params) {

                String str="";
                HttpURLConnection connection = null;
                WifiManager wifi_service = (WifiManager)getApplicationContext().getSystemService(WIFI_SERVICE);
                WifiInfo wifiInfo = wifi_service.getConnectionInfo();
                rssi=wifiInfo.getRssi();
                try {
                cnt++;
                URL url = new URL(address);
                connection = (HttpURLConnection) url.openConnection();
                // 请求方式
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(1000);
                connection.setReadTimeout(1000);
                connection.connect();
//                // 获取输入流
//                InputStream in = connection.getInputStream();
//                BufferedReader reader = new BufferedReader(
//                        new InputStreamReader(in, "UTF-8"));
//                response = new StringBuilder();
//                String line;
//                while ((line = reader.readLine()) != null) {
//                    response.append(line);
//                }
                suc++;
                } catch (Exception e) {

                } finally {
                    if (connection != null) {
                        connection.disconnect();
                    }
                }
                return  str;
            }
        };
        task.execute();
    }
    public String percent(int a,int b){
        if(b==0){
            return "0";
        }
        else {
            NumberFormat numberFormat = NumberFormat.getInstance();
            numberFormat.setMaximumFractionDigits(2);
            String c = numberFormat.format((float) a / (float) b * 100);
            return c;
        }
    }
}
